package cn.melon.mod.store.entity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

public class GoodsTable {
	/*
	 * 一类商品的列表
	 */
	private List<Goods> goodsTable = null;
	
	public GoodsTable(){
		goodsTable = new ArrayList<Goods>();
	}
	public GoodsTable(List<Goods> goodsTable) {
		setGoodsTable(goodsTable);
	}
	
	public List<Goods> getGoodsTable() {
		return goodsTable;
	}

	public void setGoodsTable(List<Goods> goodsTable) {
		this.goodsTable = goodsTable;
	}
	
	public void add(Goods goods){
		goodsTable.add(goods);
	}
	
	public Goods get(int index){
		return goodsTable.get(index);
	}
	
	public int length(){
		return goodsTable.size();
	}
	
	/**
	 * @Title: setGoodsTable
	 *  用jsonArray来设置对象Table的值
	 * @param jsonArray void
	 */
	public void setGoodsTable(JSONArray jsonArray){
	    for(int i=0;i<jsonArray.length();i++){
	        Goods goods = new Goods();
	        try {
                goods.setGoodsByJsonObj(jsonArray.getJSONObject(i));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
	        this.add(goods);
	    }
	}
	
	/**
	 * @Title: toJsonArray
	 *  转为json字符串
	 * @return JSONArray
	 */
	public JSONArray toJsonArray(){
		JSONArray jsonArray = new JSONArray();
		
		for(int i=0;i<this.length();i++){
			jsonArray.put(this.get(i).toJsonObject());
		}

		return jsonArray;
	}
}
